﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MakeTableTool
{
    /// <summary>
    /// 数据表头的定义
    /// </summary>
    public class TableField
    {
        public TableDefine define;
        public TableBaseType fieldType;

        public string strFieldType,strDefine;
        //public string toType;//用于解析定义
        public string Name;//名字
        public string Description;//描述
        public bool isBase = true; //是否为基础类型 或者自定义类型
        public bool isList = false; // 是否列表

        public TableField()
        {

        }

        #region Type
        public const String BYTE = "byte";
        public const String SHORT = "short";
        public const String INT = "int";
        public const String FLOAT = "float";
        public const String BOOL = "bool";
        public const String STRING = "string";
        public const String ULONG = "ulong";
        #endregion

        #region Define
        public const String BeTranslate = "translate";
        public const String KEY = "key";
        public const String DEL = "del";
        #endregion

        /// <summary>
        /// 设置定义
        /// </summary>
        /// <param name="strDefine"></param>
        public void SetDefine(string strDefine)
        {
            this.strDefine = strDefine;
            switch (strDefine)
            {
                case KEY:
                    define = TableDefine.Key;
                    break;
                case BeTranslate:
                    define = TableDefine.BeTranslate;
                    break;
                case DEL:
                    define = TableDefine.Delete;
                    break;
                default:
                    define = TableDefine.Null;
                    break;
            }
        }

        /// <summary>
        /// 设置字段类型
        /// </summary>
        /// <param name="strFieldType"></param>
        public void SetFieldType(string strFieldType)
        {
            strFieldType = strFieldType.Trim();
            var isArray = strFieldType.Substring(strFieldType.Length - 2);
            if (isArray.Equals("[]"))
            {
                isList = true;
            }

            if (isList)
                strFieldType = strFieldType.Substring(0, strFieldType.Length - 2);

            switch (strFieldType)
            {
                case BYTE:
                    fieldType = TableBaseType.BYTE;
                    break;
                case SHORT:
                    fieldType = TableBaseType.SHORT;
                    break;
                case INT:
                    fieldType = TableBaseType.INT;
                    break;
                case FLOAT:
                    fieldType = TableBaseType.FLOAT;
                    break;
                case BOOL:
                    fieldType = TableBaseType.BOOL;
                    break;
                case STRING:
                    fieldType = TableBaseType.STRING;
                    break;
                case ULONG:
                    fieldType = TableBaseType.ULONG;
                    break;
                default:
                    throw new Exception("字段类型配置错误");
            }

            this.strFieldType = strFieldType;
        }

        /// <summary>
        /// 读取类型
        /// </summary>
        /// <returns></returns>
        public string GetToTypeStr()
        {
            switch (strFieldType)
            {
                case BYTE:
                    return "ToByte";
                case SHORT:
                    return "ToInt16";
                case INT:
                    return "IntValue";
                case FLOAT:
                    return "ToSingle";
                case BOOL:
                    return "ToBoolean";
                case STRING:
                    return "String";
                case ULONG:
                    return "ULongValue";
                default:
                    throw new Exception("字段类型配置错误");
            }
        }

        /// <summary>
        /// 最终代码的类型
        /// </summary>
        public enum TableBaseType : byte
        {
            NULL,
            BYTE,
            SHORT,
            INT,
            FLOAT,
            BOOL,
            STRING,
            ULONG,
        }

        public enum TableDefine : byte
        {
            Null,
            Key,
            BeTranslate,
            Delete,
        }
    }

}
